package com.none.web.controller.voting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.mvc.bind.annotation.FormModel;

import com.none.core.common.utils.JSONUtil;
import com.none.core.model.Pager;
import com.none.core.model.Result;
import com.none.web.common.SysConstant;
import com.none.web.model.TActivity;
import com.none.web.model.TActivityAppUser;
import com.none.web.model.TActivityImage;
import com.none.web.service.activity.IActivityService;
import com.none.web.service.voting.IVotingService;


/**
 * voting Controller
 * @author William Wu
 * @timer  Feb 12,2015
 */
@Controller
@RequestMapping("voting")
public class VotingController {
	@Resource(name = "votingService")
	IVotingService votingService;

	@Autowired
	private HttpServletRequest request;
	
	/**
	 * check voting
	 * @param votingId
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("check")
	public Object checkVoting(String votingId,String userId)  throws Exception {
		String[] filter = new String[] {};
		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);
		if (!votingService.checkVoting(votingId, userId)) {
			result = new Result(SysConstant.STATE_ERROR, "golbal.error", data);
		}
		return JSONUtil.toJSON(result, filter);
	}
	/**
	 * join voting
	 * @param votingId
	 * @param optionsId
	 * @param itemsId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("join")
	public Object joinVoting(String votingId,String optionsId,String itemsId,String userId)  throws Exception {
		String[] filter = new String[] {};
		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);
		if (!votingService.joinVoting(votingId, userId,optionsId.split(","),itemsId.split(","))) {
			result = new Result(SysConstant.STATE_ERROR, "golbal.error", data);
		}	
		return JSONUtil.toJSON(result, filter);
	}
	
}
